def funck1(m):
  if m == 1 or m == 0:
      return False
  x = 0
  for i in range(2, int(m ** 0.5) + 1):
      if (m % i == 0):
          x = x + 1
  if (x == 0):
      return True
  else:
      return False
def funck2(a):
  a = str(a)
  results = set()
  for i in range(len(a)):
      for j in range(i, len(a)):
          results.add(a[i:j+1])
  return(results)
y = int(input())
if y < 1000 or y > 9999:
  print(0)
else:
   mas = funck2(y)
   mas = [int(x) for x in mas]
   mas.sort(reverse=True)
   for y in mas:
       if funck1(y):
           print(y)
           break
   else:
       print(0)

